import argparse as AP 
from MAETree_v4_0 import *
from MAEStats import *

#handle annotators and phases at command line
#implement extent type and link specification
#implement gold standard and strict support
#send most recent version to John and Zach

def main():
	parser = AP.ArgumentParser(description = 'A utility for various inter-annotator agreement calculations')
	parser.add_argument("path", action = "store",
		help = "specify a directory containing .xml files or text file listing such directories")	
	parser.add_argument("-m", "--mode", dest = "mode", action = "store", default = "extent_exact",
		help = "run statistical test on links (arg: l(ink)) or extents (arg: e(xtent) or e(xtent)_loose)")
	parser.add_argument("-g", "--golds", dest = "golds", action = "append", default = [],
		help = "add a gold standard file to the list for assessment of link agreement")
	parser.add_argument("-t", "--tags", dest = "tags", action = "append", default = [],
		help = "add a tag to the list of tags to be assessed (default: assess all available)")
	parser.add_argument("-a", "--annotators", action = "append", dest = "annots", default = [],
		help = "add an annotator to the annotator list (default: assess all available)")
	parser.add_argument("-p", "--phases", action = "append", dest = "phases", default = [],
		help = "add a phase to the phase list (default: assess all available)")
	parser.add_argument("-s", "--strict", action = "store_true", dest = "strict", default = True,
		help = "run in strict mode (require multiple attributes to agree)")
	parser.add_argument("-l", "--lax", action = "store_false", dest = "strict", 
		help = "run in lax mode (require that only tags agree)")
	parser.add_argument("-r", "--attributes", dest = "attrib", action = "append", default = [],
		help = "add an attribute for assessment")
	parser.add_argument("-e", "--test", dest = "test", action = "store", default = "fleiss",
		help = "determine which statistical test to run (arg: f(leiss), c(ohen), k(rippendorff))")
	parser.add_argument("-q", "--quiet", action = "store_false", dest = "verbose", default = False,
		help = "run in quiet mode (do not print file and annotator lists)")
	parser.add_argument("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
		help = "run in LOUD mode (print file and annotator lists)")
	parser.add_argument("-w", "--worst", action = "store", dest = "worst",
		help = "print a list of subjects with agreement below the specified threshold (arg: decimal between 0 and 1)")
	parser.add_argument("-c", "--correctest", action = "store", dest = "correctest",
		help = "print a list of subjects with agreement above the specified threshold (arg: decimal between 0 and 1)")
	parser.add_argument("-d", "--plenary", action = "store_true", dest = "plenary", default = False,
		help = "only consider subjects annotated by all annotators")	
	args = parser.parse_args()
	myParser = maeParser(files = args.path, annots = [i.upper() for i in args.annots], phases = [p.lower() for p in args.phases], tagnames = args.tags)
	if args.verbose:
		print myParser.filenames
		print myParser.getAnnotators()
	subjs = myParser.buildSubjectList(mode = args.mode, golds = args.golds)
	if args.plenary:
		subjs = [s for s in subjs if len(set(s.getTags().values())) == 1]
	print 'kappa:', fleissKappaTest(subjects = subjs, attrib = args.attrib, strict = args.strict).getScore()
	if args.worst:
		print '\nSubjects with agreement below ' + args.worst + ':'
		temp = [subj for subj in subjs if subj.agreement() < float(args.worst)]
		for i in temp:
			print 'file:', i.getFileName()
			print i, '\n'		
	if args.correctest:
		print '\nSubjects with agreement above ' + args.correctest + ':'
		temp = [subj for subj in subjs if subj.agreement() > float(args.correctest)]
		for i in temp:
			print 'file:', i.getFileName()
			print i, '\n'

if __name__ == "__main__":
	main()
